'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _database = require('../config/database');

var _database2 = _interopRequireDefault(_database);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

const getRecordInfoByOrderNo = (() => {
  var _ref = _asyncToGenerator(function* (orderNo) {
    let recordInfo = null;
    let sql = `SELECT * FROM yp_order_record
    WHERE order_no = ${orderNo}`;
    try {
      console.log(sql);
      recordInfo = yield _database2.default.query(sql);
      console.log(recordInfo);
    } catch (err) {
      console.log(`search yp_order_record ${err}`);
    }
    return recordInfo[0];
  });

  return function getRecordInfoByOrderNo(_x) {
    return _ref.apply(this, arguments);
  };
})();

const createRecord = (() => {
  var _ref2 = _asyncToGenerator(function* (recordInfo) {
    let recordtkeys = Object.keys(recordInfo);
    let recordtvalues = [];
    for (let key in recordtkeys) {
      recordtvalues.push(recordInfo[recordtkeys[key]]);
    }
    let stringValue = '';
    for (let k in recordtkeys) {
      stringValue += '?';
      if (parseInt(k) !== recordtkeys.length - 1) {
        stringValue += ',';
      }
    }
    let sql = `INSERT INTO yp_order_record(${recordtkeys})
    VALUES(${stringValue})`;
    try {
      console.log(sql);
      yield _database2.default.query(sql, recordtvalues);
      console.log('add yp_order_record success');
    } catch (err) {
      console.log(`create yp_order_record ${err}`);
    }
  });

  return function createRecord(_x2) {
    return _ref2.apply(this, arguments);
  };
})();

exports.default = {
  getRecordInfoByOrderNo,
  createRecord
};